/*
 * inotify-sync - a simple file synchronizer and file system watcher
 * Copyright (C) 2010-2011, inotify-sync developers and inotify-sync contributors
 * Copyright (C) 2010-2011, Cohesion Network Security Studio
 *
 * inotify-sync is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * inotify-sync is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with inotify-sync; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "profile.h"

struct popt *popts = NULL;
int popts_count = 0;
struct event_action event_action_array[12] = {
	{IN_ACCESS,"access_action",""},
	{IN_ATTRIB,"attrib_action",""},
	{IN_CLOSE_WRITE,"close_write_action",""},
	{IN_CLOSE_NOWRITE,"close_nowrite_action",""},
	{IN_CREATE,"create_action",""},
	{IN_DELETE,"delete_action",""},
	{IN_DELETE_SELF,"delete_self_action",""},
	{IN_MODIFY,"modify_action",""},
	{IN_MOVE_SELF,"move_self_action",""},
	{IN_MOVED_FROM,"moved_from_action",""},
	{IN_MOVED_TO,"moved_to_action",""},
	{IN_OPEN,"open_action",""}
};

void
profile_init()
{
	popts = (struct popt *)malloc(sizeof(struct popt) * POPT_INIT_COUNT);
	if(popts == NULL)
	{
		perror("malloc");
		exit(errno);
	}
	else
	{
		popts_count = 0;
		memset(popts, '\0', sizeof(struct popt) * POPT_INIT_COUNT);
	}
}

void
get_popt(const char *name, char *value)
{
	int i;
	for(i = 0; i < popts_count; i++)
	{
		if(!strcmp(popts[i].name, name))
		{
			strncpy(value, popts[i].value, MAX_VALUE_LEN);
			return;
		}
	}
	return;
}

void
add_popt(const char *name, const char *value)
{
	int i, j;
	for(i = 0; i < popts_count; i++)
	{
		if(strcmp(popts[i].name, name))
		{
			continue;
		}
		strncpy(popts[i].value, value, MAX_VALUE_LEN);
		for(j = 0;
			j < sizeof(event_action_array)/sizeof(struct event_action);
			j++)
		{
			if(!strcmp(event_action_array[j].name, name))
			{
				strncpy(event_action_array[j].action, value, MAX_VALUE_LEN);
				break;
			}
		}
		return;
	}

	strncpy(popts[popts_count].name, name, MAX_NAME_LEN);
	strncpy(popts[popts_count].value, value, MAX_VALUE_LEN);
	popts_count++;
	for(j = 0;
		j < sizeof(event_action_array)/sizeof(struct event_action);
		j++)
	{
		if(!strcmp(name, event_action_array[j].name))
		{
			strncpy(event_action_array[j].action, value, MAX_VALUE_LEN);
			break;
		}
	}
	return;
}
